package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ccb.ebank.pay.CcbB2cMessage;

/**
 * 
 * CcbB2cFrontRes.java
 * @author WangHongFei
 */
@Controller
@Scope("prototype")
public class CcbB2cFrontRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(CcbB2cFrontRes.class);
    @Resource(name = "ccbConfig")
	protected Map<String, String> ccbConfig;
    @RequestMapping(value = "/ccbB2cFrontRes")
    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
        logger.error("--------------- CcbB2cFrontRes bankback ---------------");
        
        return super.execute(request, response);
    }

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		CcbB2cMessage message = new CcbB2cMessage();
        // 银行码赋值
	    message.setBank(BankConstants.BANK_LOGO_CCB);
        message.setPostId(request.getParameter("POSID"));
        message.setBranchId(request.getParameter("BRANCHID"));
        message.setOrderId(request.getParameter("ORDERID"));
        message.setBankOrderId(request.getParameter("ORDERID"));
        message.setPayment(request.getParameter("PAYMENT"));
        message.setCurCode(request.getParameter("CURCODE"));
        message.setRemark1(request.getParameter("REMARK1"));
        message.setRemark2(request.getParameter("REMARK2"));
        message.setAccType(request.getParameter("ACC_TYPE"));
        message.setSuccess(request.getParameter("SUCCESS"));
        message.setType(request.getParameter("TYPE"));
        message.setReferer(request.getParameter("REFERER"));
        message.setClientIp(request.getParameter("CLIENTIP"));
        message.setSignMessage(request.getParameter("SIGN"));
        message.setAmount(new BigDecimal(message.getPayment()));
        // B2C公钥
        message.setPublicKey(ccbConfig.get("CCB.PBLIC.KEY"));
        if (StringUtils.equals(ccbConfig.get("CCB.INCHARAGE.POSID"), message.getPostId())) {
            // 信用卡公钥
            message.setPublicKey(ccbConfig.get("CCB.INCHARAGE.PBLIC.KEY"));
        }
        if (StringUtils.equals(ccbConfig.get("CCB.SMALL.POSID"), message.getPostId())) {
            // 小额公钥
            message.setPublicKey(ccbConfig.get("CCB.SMALL.PBLIC.KEY"));
        }
        if (StringUtils.equals(ccbConfig.get("CCB.THREE.POSID"),
                message.getPostId())) {
            // 大额公钥
            message.setPublicKey(ccbConfig.get("CCB.THREE.PBLIC.KEY"));
        }
        if(StringUtils.equals(ccbConfig.get("CCB.MOBILE.POSID"),
                message.getPostId())){
            //手机支付柜台
            message.setPublicKey(ccbConfig.get("CCB.MOBILE.PBLIC.KEY"));
        }
        message.setFront(true);
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
        return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request,Result result) {		
		OrderInfo orderInfo = new OrderInfo(); 
		CcbB2cMessage message = (CcbB2cMessage) result.getModel("res");
		if(message!=null){
			if(message.getSuccess().equals("Y")){
				orderInfo.setOrderId(message.getOrderId());
			    orderInfo.setAmt(message.getPayment());
			    orderInfo.setNotifyNum("1");
		        orderInfo.setPayStatus("00");
		        orderInfo.setRetCode("00");
		        orderInfo.setRetMsg("");
			}else{
				 orderInfo.setOrderId(message.getOrderId());
		         orderInfo.setAmt(message.getPayment());
		         orderInfo.setNotifyNum("1");
		         orderInfo.setPayStatus("01");
		         orderInfo.setRetCode("01");
		         orderInfo.setRetMsg("");
			}
		}
        return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("POSID"))
				|| StringUtils.isEmpty(request.getParameter("BRANCHID"))
				|| StringUtils.isEmpty(request.getParameter("ORDERID"))
				|| StringUtils.isEmpty(request.getParameter("PAYMENT"))
				|| StringUtils.isEmpty(request.getParameter("SUCCESS"))
				|| StringUtils.isEmpty(request.getParameter("SIGN"))) {
			return false;
		}
		return true;
	}
}
